package cn.bocweb.spinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ErrorBean;
import cn.bocweb.spinning.model.Bean.FindPwdBean;
import cn.bocweb.spinning.model.Bean.FindVerifyBean;
import cn.bocweb.spinning.presenter.Imp.UserPresenterImp;
import cn.bocweb.spinning.presenter.UserPresenter;
import cn.bocweb.spinning.util.Constant;
import cn.bocweb.spinning.util.DeviceUtil;
import cn.bocweb.spinning.view.common.BaseActivity;
import cn.bocweb.spinning.view.common.FindPwdVerifyButton;
import de.greenrobot.event.EventBus;

public class ForgetPwdActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.forget_pwd_phone)
    EditText forgetPwdPhone;
    @Bind(R.id.forget_pwd_verify)
    EditText forgetPwdVerify;
    @Bind(R.id.forget_pwd_btn_verify)
    FindPwdVerifyButton forgetPwdBtnVerify;
    @Bind(R.id.forget_pwd_newpwd)
    EditText forgetPwdNewpwd;
    @Bind(R.id.forget_pwd_confirmpwd)
    EditText forgetPwdConfirmpwd;
    @Bind(R.id.forget_pwd_btn_submit)
    Button forgetPwdBtnSubmit;

    UserPresenter presenter;
    int forgetPwdState;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_pwd);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        presenter = new UserPresenterImp();
        initIntent();
        initVerify();
        initToolbar();
        initClickBtn();
    }

    private void initIntent() {
        Intent intent = getIntent();
        forgetPwdState = intent.getIntExtra(Constant.TO_FORGET_PWD, 0);
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }

    }

    private void initVerify() {
        forgetPwdBtnVerify.setTextAfter("秒", this).setTextBefore("获取验证码", this).setLenght(60*1000);
    }

    private void initClickBtn() {
        forgetPwdBtnSubmit.setOnClickListener(this);
        forgetPwdBtnVerify.setOnClickListener(this);
    }

    private void initToolbar() {
        toolbar.setTitle("忘记密码");
        toolbar.setTitleTextColor(getResources().getColor(R.color.white));
        toolbar.setNavigationIcon(R.mipmap.btn_exit);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.forget_pwd_btn_submit: {
                SubmitForgetPwd();
                break;
            }
            case R.id.forget_pwd_btn_verify: {
                if (TextUtils.isEmpty(forgetPwdPhone.getText().toString())) {
                    DeviceUtil.MyToast(this, "请输入手机号码...");
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                    break;
                }
                if (!(forgetPwdPhone.getText().toString().subSequence(0, 1).equals("1") && forgetPwdPhone.getText().toString().length() == 11)) {
                    DeviceUtil.MyToast(this, "请输入正确的手机号...");
                    if (dialog.isShowing()) {
                        dialog.dismiss();
                    }
                    break;
                }
                presenter.findVerify(forgetPwdPhone.getText().toString());
                break;
            }
        }
    }

    private void SubmitForgetPwd() {
        showLoading();
        String phone = forgetPwdPhone.getText().toString();
        String pwd = forgetPwdNewpwd.getText().toString();
        String confirmpwd = forgetPwdConfirmpwd.getText().toString();
        String verify = forgetPwdVerify.getText().toString();
        if (TextUtils.isEmpty(phone)) {
            DeviceUtil.MyToast(this, "请输入手机号码...");
            hideLoading();
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            DeviceUtil.MyToast(this, "请输入密码...");
            hideLoading();
            return;
        }
        if (TextUtils.isEmpty(verify)) {
            DeviceUtil.MyToast(this, "请输入验证码...");
            hideLoading();
            return;
        }
        if (!(phone.subSequence(0, 1).equals("1") && phone.length() == 11)) {
            DeviceUtil.MyToast(this, "请输入正确的手机号...");
            hideLoading();
            return;
        }
        if (!pwd.equals(confirmpwd)) {
            DeviceUtil.MyToast(this, "两次输入的密码不一致哦...");
            hideLoading();
            return;
        }
        presenter.findPwd(phone, pwd, confirmpwd, verify);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 忘记密码提交
     */

    public void onEventMainThread(FindPwdBean bean) {
        hideLoading();
        DeviceUtil.MyToast(this, bean.getMsg());
        bean.getData().writeInfoIntoLocal(this);
        if (bean.getReturn_code() == 0) {
            switch (forgetPwdState) {
                case Constant.TRAIN_TO_LOGIN: {
                    setResult(Constant.TRAIN_TO_LOGIN_FORGET_PWD);
                    finish();
                    break;
                }
                case Constant.SEQUENCE_TEST_COLLECT: {
                    setResult(Constant.SEQUENCE_TEST_COLLECT_FORGET_PWD);
                    finish();
                    break;
                }
                case Constant.SEQUENCE_TEST_SYNC: {
                    setResult(Constant.SEQUENCE_TEST_SYNC_FORGET_PWD);
                    finish();
                    break;
                }
                case Constant.INFODETAIL_TO_LOGIN: {
                    setResult(Constant.LOGIN_TO_MAIN_FORGET_PWD);
                    finish();
                    break;
                }
                default: {
                    Intent intent = new Intent(this, MainActivity.class);
                    startActivity(intent);
                }
            }

        }
    }

    /**
     * 忘记密码验证码
     */
    public void onEventMainThread(FindVerifyBean bean) {
        DeviceUtil.MyToast(this, bean.getMsg());
        if (bean.getReturn_code() == 0) {
            DeviceUtil.MyToast(this, bean.getMsg());
            forgetPwdBtnVerify.startTime();
        }
        hideLoading();
    }

    public void onEventMainThread(ErrorBean bean) {
        hideLoading();
        DeviceUtil.MyToast(this, bean.getErrorMsg());
    }
}
